<template>
  <div class="logAndReg-container" ref="index" :style="{ backgroundImage: 'url(' + url + ')' }">
    <div class="head-box">
      <div class="bacground-box" @click="changeImg" style="transform: scale(0.95)">
        <van-icon name="photo" color="#ffbf05" size="21" style="margin-right: 0.3em" />
        <span :style="{ color: headTextColor ? 'white' : '#3d3d3d' }">{{ $t('u.BG') }}</span>
      </div>
      <div class="language-box" style="transform: scale(0.95)" @click="changeLanguage">
        <span :style="{ color: headTextColor ? 'white' : '#3d3d3d' }">{{ $t('u.lang') }}</span>
        <van-icon name="exchange" color="#ffbf05" size="21" style="margin-left: 0.3em" />
      </div>
    </div>
    <!-- login & register slot -->
    <div class="input-box">
      <slot name="input_slot"> </slot>
    </div>
    <div class="logReg-shift">
      <slot name="shift_slot"> </slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LogAndReg',
  data() {
    return {
      imgArr: [require('../../assets/bg_img/bg_1.jpg'), require('../../assets/bg_img/bg_2.jpg')],
      url: require('../../assets/bg_img/bg_1.jpg'),
      headTextColor: true,
      language: ''
    }
  },
  created() {
    this.$store.dispatch('getLang').then(result => {
      if (result.code !== 200) return (this.language = 'zh-CN')
      this.$i18n.locale = result.data.lang
      this.language = result.data.lang
      console.log('获取语言偏好成功')
    })
  },
  methods: {
    changeImg() {
      if (this.url === this.imgArr[0]) {
        this.url = this.imgArr[1]
        this.headTextColor = false
      } else {
        this.url = this.imgArr[0]
        this.headTextColor = true
      }
    },
    // 发起切换语言请求
    changeLanguage() {
      var lang = this.language
      if (lang === 'zh-CN') {
        lang = 'en-US'
      } else {
        lang = 'zh-CN'
      }
      this.$i18n.locale = lang
      var params = new URLSearchParams()
      params.append('lang', lang)
      this.$store.dispatch('setLang', params).then(msg => {
        if (msg) return this.$toast.fail('切换语言失败')
        console.log('切换成功')
      })
    }
  }
}
</script>

<style lang="less">
.logAndReg-container {
  position: fixed;
  width: 100%;
  height: 100%;
  max-width: 640px;
  min-height: 100%;
  background-size: cover;
  background-repeat: no-repeat;
  .head-box {
    display: flex;
    justify-content: space-between;
    width: 90%;
    margin: 30px auto;
    color: white;
    .bacground-box,
    .language-box {
      display: flex;
      align-items: center;
    }
  }
}
.input-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: absolute;
  width: 97%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 9px;
  background: white;
  #title,
  #username,
  #password,
  #logReg-btn,
  #log-mode {
    display: flex;
    align-items: center;
    width: 93%;
    height: 60px;
    .van-button--primary {
      border-radius: 6px;
      font-size: 1.1em;
      background: #ffbf05;
      border: none;
    }
  }
  #user-box,
  #pwd-box {
    display: flex;
    align-items: center;
    border-radius: 6px;
    width: 100%;
    height: 42px;
    border: 1px solid #9e9e9e;
    .van-cell {
      position: relative;
      display: flex;
      align-items: center;
      height: 40px;
      border-radius: 6px;
    }
  }
}
.logReg-shift {
  position: absolute;
  width: 97%;
  height: 90px;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  border-top-left-radius: 9px;
  border-top-right-radius: 9px;
  background: white;
  z-index: -1;
  .van-button--default {
    position: absolute;
    width: 95%;
    left: 50%;
    margin-top: 0.6em;
    transform: translateX(-50%);
    border: 1px solid #ffbf05;
    border-radius: 9px;
    font-size: 1em;
  }
  .beian-info {
    position: absolute;
    width: 100%;
    height: 30px;
    line-height: 30px;
    bottom: 0;
    text-align: center;
    font-size: 13px;
    a {
      color: #929292;
      text-decoration: underline;
    }
  }
}
</style>
